package com.xnccc.view;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.ViewConfiguration;
import android.widget.SeekBar;

public class VerticalSeekBar extends SeekBar
{
    private boolean isInScrollingContainer = false;

    public interface OnSeekBarChangeListener 
    {
        void onProgressChanged(VerticalSeekBar VerticalBar, int progress, boolean fromUser);
        void onStartTrackingTouch(VerticalSeekBar VerticalBar);
        void onStopTrackingTouch(VerticalSeekBar VerticalBar);
    }

    private OnSeekBarChangeListener mOnSeekBarChangeListener;

    public void setOnSeekBarChangeListener(OnSeekBarChangeListener l) 
    {
        mOnSeekBarChangeListener = l;
    }

    public boolean isInScrollingContainer()
    {
        return isInScrollingContainer;
    }

    public void setInScrollingContainer(boolean isInScrollingContainer)
    {
        this.isInScrollingContainer = isInScrollingContainer;
    }

    /**
     * On touch, this offset plus the scaled value from the position of the
     * touch will form the progress value. Usually 0.
     */
    float mTouchProgressOffset;

    public VerticalSeekBar(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
        ViewConfiguration.get(context).getScaledTouchSlop();
        this.setMax(20);
    }

    public VerticalSeekBar(Context context, AttributeSet attrs)
    {
        super(context, attrs);
    }

    public VerticalSeekBar(Context context)
    {
        super(context);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh)
    {

        super.onSizeChanged(h, w, oldh, oldw);
    }

    @Override
    protected synchronized void onMeasure(int widthMeasureSpec,
            int heightMeasureSpec)
    {
        super.onMeasure(heightMeasureSpec, widthMeasureSpec);
        setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth());
    }

    @Override
    protected synchronized void onDraw(Canvas canvas)
    {
        canvas.rotate(90);
        canvas.translate(0, -getWidth());
        super.onDraw(canvas);        
    }

   

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
    	int code = keyCode;
    	int progress;
		switch (keyCode) {
		case KeyEvent.KEYCODE_DPAD_LEFT:
			code = KeyEvent.KEYCODE_DPAD_DOWN;
			break;
		case KeyEvent.KEYCODE_DPAD_RIGHT:
			code = KeyEvent.KEYCODE_DPAD_UP;
			break;
		case KeyEvent.KEYCODE_DPAD_UP:
			code = KeyEvent.KEYCODE_DPAD_LEFT;
			progress = getProgress();
            if (progress <= 0) break;
            setProgress(progress-1);
            if (mOnSeekBarChangeListener != null) {  
                mOnSeekBarChangeListener.onProgressChanged(this, getProgress(), true);  
            }  
            return true;
		case KeyEvent.KEYCODE_DPAD_DOWN:
			code = KeyEvent.KEYCODE_DPAD_RIGHT;
			progress = getProgress();
            if (progress > getMax()-1) break;
            setProgress(progress+1);
            if (mOnSeekBarChangeListener != null) {  
                mOnSeekBarChangeListener.onProgressChanged(this, getProgress(),true);  
            }  
            return true;
		}
    	
    	return super.onKeyDown(code, event);
    }
    
    @Override
    public synchronized void setProgress(int progress)
    {
        super.setProgress(progress);
        onSizeChanged(getWidth(), getHeight(), 0, 0);
        if (mOnSeekBarChangeListener != null) {  
            mOnSeekBarChangeListener.onProgressChanged(this, getProgress(), isPressed());  
        }  
    }

}
